package com.manage.servlet;

import com.manage.pojo.Housing;
import com.manage.pojo.ResultData;
import com.manage.service.HousingService;
import com.manage.service.impl.HousingServiceImpl;
import com.manage.utils.ExcelUtils;
import com.manage.utils.FileUploadUtils;
import com.manage.utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/housing")
@MultipartConfig
public class HousingServlet extends BaseServlet{
    //分页展示小区
    public ResultData all(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");

        //2.处理请求
        HousingService housingService=new HousingServiceImpl();
        ResultData resultData = housingService.selectByPage(page, limit);


        //3.响应请求
        return resultData;
    }
//删除一个小区
    public ResultData del(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String hid = request.getParameter("hid");


        //2.处理请求
        HousingService housingService=new HousingServiceImpl();
        ResultData resultData = housingService.removeOneByHid(hid);


        //3.响应请求
        return resultData;
    }
//    缩略图上传
    public ResultData upload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        //保存到数据库中的名称
        String fileName = FileUploadUtils.uploadFile(req, "file");
        //3、做出响应
        return new ResultData(0,"成功",fileName);
    }

    //添加一个小区
    public ResultData add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String housing = request.getParameter("housing");
        Housing housing1 = JsonUtils.jsonToObject(housing, Housing.class);

        //2.处理请求
        HousingService housingService=new HousingServiceImpl();
        ResultData resultData = housingService.addHousing(housing1);


        //3.响应请求
        return resultData;
    }
    //删除勾选的所有小区
    public ResultData deleteAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //1、处理请求
        String housings = req.getParameter("housings");
        List<Housing> empList = JsonUtils.jsonToList(housings, Housing.class);
        //

        //2、调用业务层方法
        HousingService housingService = new HousingServiceImpl();
        ResultData resultData = housingService.deleteAllEmp(empList);

        //3、做出响应
        return resultData;
    }
//编辑展示
    public ResultData one(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //1、处理请求
        String hid = req.getParameter("hid");

        //2、调用业务层方法
        HousingService housingService = new HousingServiceImpl();
        ResultData resultData = housingService.findOne(hid);

        //3、做出响应
        return resultData;
    }
    //编辑修改
    public ResultData update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //1、处理请求
        String housingjson = req.getParameter("housing");
        Housing housing = JsonUtils.jsonToObject(housingjson, Housing.class);

        //2、调用业务层方法
        HousingService housingService = new HousingServiceImpl();
        ResultData resultData = housingService.updateEmp(housing);

        //3、做出响应
        return resultData;
    }
//导入Excel数据
    public ResultData uploadExcel(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        //获取文件上传对象 getPart
        Part part = request.getPart("file");
        //通过part对象回去Excel文件的输入流对象
        InputStream inputStream = part.getInputStream();
        //获取文件后缀
        String exc = part.getSubmittedFileName().substring(part.getSubmittedFileName().lastIndexOf("."));
        //获取导入数据
        List<String[]> datas = ExcelUtils.importData(exc, inputStream, 1);
        //2.处理请求
        HousingService housingService=new HousingServiceImpl();
        ResultData resultData = housingService.addHousingList(datas);
        //3.响应请求
        return resultData;
    }

    //导出Excel数据
    public void download(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //准备表格头
        String[] titles={"ID","小区编号","小区名称","坐落地址","占地面积(m²)","总栋数","总户数","绿化率(%)","缩略图","开发商名称","物业公司名称","创建时间"};
        HousingService housingService=new HousingServiceImpl();
        List<String[]> data = housingService.getData();

        //设置响应头，告知浏览器是下载操作
        response.setHeader("Content-Disposition","attachment;filename=housing.xls");
        //设置MIME类型
        response.setContentType("application/vnd.ms-excel");
        ExcelUtils.export(titles,data,response.getOutputStream());
    }
//搜索
    public ResultData search(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        String hname = request.getParameter("hname");
        String hhiredate = request.getParameter("hhiredate");
        //2.处理请求
        HousingService housingService=new HousingServiceImpl();
        ResultData resultData = housingService.searchAll(page, limit,hname,hhiredate);


        //3.响应请求
        return resultData;
    }
}
